/**
 * class name: pop$,
 * copyright: maximus zhang,
 * License: MIT-LICENSE,
 * contact: maximus.zhang@gmail.com,
 * required-js: http://hcjs.googlecode.com/svn/trunk/hc.js, mask.js,
 * update: http://hcjs.googlecode.com/svn/trunk/slide.js
 *
        var ifm='<iframe style="position:absolute;width:2048px;height:1536px;top:50%;left:50%;margin:-768px 0 0 -1024px;border:none 0;z-index:99;" scrolling="no" frameborder="0"></iframe>';
        _.$.innerHTML=ifm;
 */
function pop$(){/* {id,css,ico,txt,x,data} */
    var A = arguments,
        _ = this;
    _ = ext$( _, ext$({
        id : 'pop'+ now$(3),
        css : 'position:absolute;top:0;left:0;',
        ico : {src:'',css:''},
        close : '/icos/ishut.gif',
        txt : '',
        nomask : false,
        cmdcss : 'width:75px;height:28px;margin:0 6px 0 0;',
        type : '',
        data : ''
        }, A[0] ) );
    _.uitype = 'pop$';
    _.bound = {};
    _.z = now$().getFullYear()+1;

    _.move$ = function(ev){
        var ev = evt$(ev);
        me.all && _.tit.setCapture();
        _.pz.left = ev.X-_.pz._x<=0 || _.pz.W<=_.pz.width ? 0 : ev.X-_.pz._x+_.pz.width>=_.pz.W ? _.pz.W-_.pz.width : ev.X-_.pz._x;
        _.pz.top = ev.Y-_.pz._y<=0 || _.pz.H<=_.pz.height ? 0 : ev.Y-_.pz._y+_.pz.height>=_.pz.H ? _.pz.H-_.pz.height : ev.Y-_.pz._y;
        _.$.style.top = _.pz.top +'px';
        _.$.style.left = _.pz.left +'px';
    };

    _.stop$ = function(){
        try {
            unbind$(me,'mousemove',_.move$);
            unbind$(me,'mouseup',_.stop$);
            if ( $$.msie ) {
                me.all && _.tit.releaseCapture();
                me.onselectstart = function(){;};
            }
        } catch (e) {}
    };

    _.tomove$ = function(ev){
        if ( $$.msie ) {
            me.onselectstart = function(){
                me.selection.empty();
                event.returnValue = false;
            };
        }
        var ev = evt$(ev);
        _.pz._y = ev.Y-_.pz.top;
        _.pz._x = ev.X-_.pz.left;
        bind$(me,'mousemove',_.move$);
        bind$(me,'mouseup',_.stop$);
    };

    _.size$ = function(){
        _.$.style.cssText = _.css;
        _.pz = pos$(_.$);
        _.$.style.top = _.pz.H<=_.pz.height ? 0 : (_.pz.H-_.pz.height)/2 +'px';
        _.$.style.left = _.pz.W<=_.pz.width ? 0 : (_.pz.W-_.pz.width)/2 +'px';
        _.pz = pos$(_.$);
    return _;
    };

    _.setype$ = function(t){
        if ( !/normal|alert|confirm/i.test(t) ) {
            return _;
        }

        _.size$();
        _.body.style.height = _.pz.height-(/normal/i.test(t)?28:60) +'px';
        _.body.style.width = _.pz.width-2 +'px';

        if ( /normal/i.test(t) ) {
            if ( typeof _.bound.yes=='function' ) {
                unbind$(_.yes,'click',_.bound.yes);
                delete _.bound.yes;
            }

            if ( typeof _.bound.no=='function' ) {
                unbind$(_.no,'click',_.bound.no);
                delete _.bound.no;
            }

            if ( typeof _.bound.xx=='function' ) {
                unbind$(_.xx,'click',_.bound.xx);
                delete _.bound.xx;
            }

            _.cmd && _.cmd.empty$() && _.$.removeChild(_.cmd) && delete _.cmd;
            _.no && delete _.no;
            _.yes && delete _.yes;
            _.type = 'normal';
        return _;
        }

        if ( !_.cmd ) {
            _.cmd = el$('div').attr$('align','right').appendTo$(_.$).class$('cmd');
        }

        if ( !hop(_,'yes')||_.yes.nodeName!='BUTTON' ) {
            _.yes = el$('button').attr$('type','button').appendTo$(_.cmd);
        }

        _.yes.style.cssText = _.cmdcss;
        _.yes.innerHTML = '确定';
        _.yes.onclick = _.hide$;
        _.type = 'alert';

        if ( t=='confirm' ) {
            if ( !hop(_,'no') || _.no.nodeName!='BUTTON' ) {
                _.no = el$('button').attr$('type','button').appendTo$(_.cmd);
            }
            _.no.style.cssText = _.cmdcss;
            _.no.innerHTML = '取消';
            _.no.onclick = _.hide$;
            _.type = 'confirm';
        }
    return _;
    };

    _.setmsg$ = function(msg){
        _.body.empty$();
        _.titxt.empty$();
        switch ( _.type ) {
            case 'alert':
                _.icon.style.backgroundImage = 'url(/icos/information.png)';
                _.titxt.innerHTML = 'Alert ....';

                if ( !_.msg || _.msg.nodeName!='DIV' ) {
                    _.msg = el$('div').class$('msg');
                }
                _.msg.appendTo$(_.body);
                break;
            case 'confirm':
                _.icon.style.backgroundImage = 'url(/icos/exclamation.png)';
                _.titxt.innerHTML = 'Confirm ....';

                if ( !_.msg || _.msg.nodeName!='DIV' ) {
                    _.msg = el$('div').class$('msg');
                }
                _.msg.appendTo$(_.body);
                break;
            default:
                if ( hop(_,'msg') ) {
                    delete _.msg;
                }
                _.icon.style.backgroundImage = 'url('+ _.ico.src +')';
                _.titxt.innerHTML = _.txt;
                break;
        }

        var box = _.msg || _.body;
        if ( typeof msg =='function' ) {
            _.htm = msg.apply(box,[_.$]);
            if ( typeof _.htm=='string' ) {
                box.innerHTML = _.htm;
            }
        } else if ( typeof msg=='string' ) {
            box.innerHTML = msg;
        } else if ( msg.nodeType ) {
            box.appendChild(msg);
        }

    return _;
    };

    _.alert$ = function(msg){
        if ( _.type=='confirm' ) {
            if ( typeof _.bound.yes=='function' ) {
                unbind$(_.yes,'click',_.bound.yes);
                delete _.bound.yes;
            }

            if ( typeof _.bound.no=='function' ) {
                unbind$(_.no,'click',_.bound.no);
                delete _.bound.no;
            }
            _.cmd.removeChild(_.no);
            _.no = null;
            delete _.no;

            if ( typeof _.bound.xx=='function' ) {
                unbind$(_.xx,'click',_.bound.xx);
                delete _.bound.xx;
            }
        }
        _.setype$('alert');
        _.setmsg$(msg);
    return _.show$();
    };

    _.confirm$ = function(msg,fy,fx){
        if ( _.type!='confirm' ) {
            _.setype$('confirm');

            if ( typeof fy=='function' ) {
                bind$(_.yes,'click',fy);
                _.bound.yes = fy;
            }

            if ( typeof fx=='function' ) {
                bind$(_.no,'click',fx);
                bind$(_.xx,'click',fx);
                _.bound.xx = _.bound.no = fx;
            }
            _.setmsg$(msg);
        }
    return _.show$();
    };

    _.show$ = function(msg){
        if ( typeof msg=='object' ) {
            if ( hop(msg,'ico') ) {
                _.ico.src = msg.ico;
            }

            if ( hop(msg,'txt') ) {
                _.txt = msg.txt;
            }

            if ( hop(msg,'css') ) {
                _.css = msg.css;
            }

            _.setype$('normal');
            hop(msg,'data') && _.setmsg$(msg.data);
        }

        _.mask && _.mask.on$();
        _.$.style.zIndex = _.z;
        _.$.style.display = '';
    return _;
    };

    _.hide$ = function(){
        _.mask && _.mask.off$();
        _.$.style.zIndex = -1;
        _.$.style.display = 'none';
    return _;
    };

    _.init$ = function(){
        _.$ = el$('div').before$(me.body.childNodes[0]).class$('popup');
        _.$.style.cssText = _.css;
        _.$.style.zIndex = -1;
        _.id = _.$.id = p$(_,'id') && !$(_.id) ? _.id : ('pop_'+ now$(3));

        _.size$();

        if ( !_.nomask ) {
            _.mask = new mask$({id:(_.id+'masker')}).init$().off$();
        }

        if ( $$.msie && $$.version<7.0 ) {
            var ifm = el$('iframe');
            ifm.frameBorder = '0';
            ifm.scrolling = 'no';
            _.$.appendChild(ifm);
            with ( ifm.style ) {
                position = 'absolute';
                top = left = '0';
                width = _.pz.width +'px';
                height = _.pz.height +'px';
                marginTop = '0';
                marginLeft = '0';
                border = '0';
                zIndex = '-1';
                overflow = 'hidden';
            }

            _._ = el$('div').appendTo$(_.$);
            _._.style.cssText = 'position:absolute;width:100%;height:100%;z-index:'+ _.z +';';
        }

        /* popup title */
        _.tit = el$('div').class$('tit').appendTo$(_.$);
        _.tit.style.zIndex = _.z;
        bind$(_.tit,'mousedown',_.tomove$);

        _.icon = el$('span').class$('ico').appendTo$(_.tit);
        _.icon.style.cssText = 'background:url('+ _.ico.src +') 0 0 no-repeat;';

        _.titxt = el$('div').class$('txt').appendTo$(_.tit);

        _.xx = el$('span').class$('xx').appendTo$(_.tit);
        _.xx.style.cssText = 'background:url('+ _.close +') 0 0 no-repeat;';

        _.xx.onmouseover = function(){
            this.style.backgroundPosition = '-16px 0';
        };

        _.xx.onmouseout = function(){
            this.style.backgroundPosition = '0 0';
        };
        _.xx.onclick = _.hide$;

        /* popup body */
        _.body = el$('div').class$('ctnt').appendTo$(_.$);
        with ( _.body.style ) {
            width = _.pz.width-2 +'px';
            height = _.pz.height-28 +'px';
            zIndex = _.z;
        }

        _.setmsg$(_.data);

    return _.hide$();
    };

}